﻿using Data;
using LoginUI.Login;
using LoginUI.UI;
using UnityEngine;
using UnityEngine.U2D;
using UnityEngine.UI;

namespace Login.Server
{
    public class ServerRightItem : MonoBehaviour
    {
        // 区是否新状态,Image组件
        public Image imgNew;

        // 区名称
        public Text txtName;

        // 区状态 Image组件
        public Image imgState;
        public Button buttonSelf;
        private ServerInfo _serverInfo;

        private void Init()
        {
            buttonSelf.onClick.AddListener(() =>
            {
                LoginMgr.Instance.LoginData.lastServerId = _serverInfo.id;
                LoginMgr.Instance.SaveLoginData();
                UIManager.Instance.HidePanel<ChooseServerPanel>();
                // 销毁??
                var panel = UIManager.Instance.ShowPanel<ServerPanel>();
                panel.ChangeInfo(_serverInfo);
            });
        }

        public void ChangeInfo(ServerInfo serverInfo)
        {
            Init();
            _serverInfo = serverInfo;
            // 区名称
            txtName.text = serverInfo.id + "区 " + serverInfo.name;
            // 区是否新状态
            if (serverInfo.isNew)
                imgNew.gameObject.SetActive(true);
            else
                imgNew.gameObject.SetActive(false);
            // 区状态
            if (serverInfo.state == 1) imgState.gameObject.SetActive(true);

            var spriteAtlas = Resources.Load<SpriteAtlas>("UI/SpriteAtlas/Login");
            imgState.gameObject.SetActive(true);
            switch (serverInfo.state)
            {
                case 0:
                    imgState.gameObject.SetActive(false);
                    break;
                case 1:
                    imgState.sprite = spriteAtlas.GetSprite("ui_DL_liuchang_01");
                    break;
                case 2:
                    imgState.sprite = spriteAtlas.GetSprite("ui_DL_fanhua_01");
                    break;
                case 3:
                    imgState.sprite = spriteAtlas.GetSprite("ui_DL_huobao_01");
                    break;
                case 4:
                    imgState.sprite = spriteAtlas.GetSprite("ui_DL_weihu_01");
                    break;
            }
        }
    }
}